package com.llkj.common.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.llkj.common.R;
import com.llkj.common.widget.LoadingView;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/4/24
 * desc  :
 */
public class LoadingDialog extends Dialog {

    private Context context;

    public LoadingDialog(@NonNull Context context) {
        this(context, R.style.TipDialog);
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tip_dialog_layout);
        ViewGroup contentWrap = findViewById(R.id.contentWrap);
        LoadingView loadingView = new LoadingView(context);
        loadingView.setColor(Color.WHITE);
        loadingView.setSize(ConvertUtils.dp2px(32));
        LinearLayout.LayoutParams loadingViewLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        loadingView.setLayoutParams(loadingViewLP);
        contentWrap.addView(loadingView);

        TextView tipView = new TextView(context);
        LinearLayout.LayoutParams tipViewLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tipViewLP.topMargin = ConvertUtils.dp2px(12);
        tipView.setLayoutParams(tipViewLP);
        tipView.setEllipsize(TextUtils.TruncateAt.END);
        tipView.setGravity(Gravity.CENTER);
        tipView.setMaxLines(2);
        tipView.setTextColor(ContextCompat.getColor(context, R.color.white));
        tipView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        tipView.setText("正在加载");
        contentWrap.addView(tipView);
        initDialogWidth();
    }

    private void initDialogWidth() {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams wmLp = window.getAttributes();
            wmLp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            window.setAttributes(wmLp);
        }
    }
}
